package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.converter.AccountConverter;
import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.City;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Country;
import com.asset.salesforce.domain.DeletedAccount;
import com.asset.salesforce.domain.Employee;
//import com.asset.salesforce.domain.FollowType;
//import com.asset.salesforce.domain.Industry;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.domain.Post;
//import com.asset.salesforce.domain.Type;
import com.asset.salesforce.domain.UpdateChatter;
//import com.asset.salesforce.domain.UpdateType;
import com.asset.salesforce.enums.FOLLOWTYPE;
import com.asset.salesforce.enums.INDUSTRY;
import com.asset.salesforce.enums.TYPE;
import com.asset.salesforce.enums.UPDATETYPE;
import com.asset.salesforce.form.AccountForm;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CheckingUtilsService;
import com.asset.salesforce.service.CityService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.DeletedAccountService;
//import com.asset.salesforce.service.FollowTypeService;
//import com.asset.salesforce.service.IndusteryService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.LeadService;
import com.asset.salesforce.service.PostService;
//import com.asset.salesforce.service.TypeService;
import com.asset.salesforce.service.UpdateChatterService;
//import com.asset.salesforce.service.UpdateTypeService;

@Controller
public class AccountController {
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private LeadService leadService;
//	
//	@Autowired
//	private IndusteryService industeryService;
//	
//	@Autowired 
//	private TypeService typeService;
//	
	@Autowired 
	private CountryService countryService;
	
	@Autowired
	private CityService cityService;
	@Autowired
	private UpdateChatterService updateChatterService;
//	
//	@Autowired
//	private UpdateTypeService updateTypeService;
//	
	@Autowired
	private PostService postService;
	
//	@Autowired
//	private FollowTypeService followTypeService;
//	
	@Autowired
	private DeletedAccountService deletedAccountService;
	
	@Autowired
	private CheckingUtilsService checkingUtilsService;
	
	@Autowired
	private JoinRequestService joinRequestService;
	@Autowired
	private CalendarService calendarService;
	
	@RequestMapping(value="/accounts", method=RequestMethod.GET)
	public String getAccount(Model model,HttpSession session,
			@RequestParam(value="index",required=false)Integer index,
			@RequestParam(value="pages",required=false)Integer pages,
			@RequestParam(value="count",required=false)Integer count) {
		if(null ==session.getAttribute("userDetails") ){
		return "redirect:/s/login.html";
	}
		if(count ==null){
			count = 5 ;
		}
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		model.addAttribute("userDetails",userDetails);
		List<Account> accounts = new ArrayList<Account>();
		if(index != null){
			accounts = accountService.getAllAccountForCompany(index,userDetails.getCompany().getCompanyName(),count);
			model.addAttribute("index", index);
			model.addAttribute("pages", pages);
			model.addAttribute("count", count);
		}else{
			Integer size = accountService.getAllAccountForCompanySize(userDetails.getCompany().getCompanyName());
			Integer numOfPages = size/count;
			if(size % count > 0){
				numOfPages++;
			}
			accounts = accountService.getAllAccountForCompany(0,userDetails.getCompany().getCompanyName(),count);
			model.addAttribute("index", 0);
			model.addAttribute("pages", numOfPages);
			model.addAttribute("count", count);
		}			
		for (Account account : accounts) {
			account.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatAccount(userDetails, account));
		}
		System.out.println("000000000000000000000000000000000000000"+accounts.size());
		model.addAttribute("accounts", accounts);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "accounts";
	}
	
	@RequestMapping(value="/accounts", method=RequestMethod.POST)
	public String postAccount() {
		
		return "accounts";
	}
	@RequestMapping(value="/new-account",method=RequestMethod.GET)
	public String getNewLead(Model model,HttpSession session, ModelMap modelMap){
		if (SecurityUtils.getSubject().hasRole("accountAdd")){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		model.addAttribute("userDetails",userDetails);
		TYPE[] types =TYPE.values();
		INDUSTRY[] industries = INDUSTRY.values();
		AccountForm accountForm = new AccountForm();
		
		modelMap.addAttribute("accountForm", accountForm);
		model.addAttribute("industries", industries);
		model.addAttribute("types", types);
		List<Country> countries = new ArrayList<Country>();
		countries =  countryService.getAll();
		model.addAttribute("countries", countries);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "new-account";
		}
		else{
			return "accessDenied";
		}
	}
	
	@RequestMapping(value="/new-account",method=RequestMethod.POST)
	public String postNewAccount(Model model,HttpSession session,
			/*@RequestParam(value="account_name",required=false)String account_name,
			@RequestParam(value="parentAccount",required=false)String parentAccount,
			@RequestParam(value="phone",required=false)String phone,
			@RequestParam(value="fax",required=false)String fax,
			@RequestParam(value="website",required=false)String website,
			@RequestParam(value="street",required=false)String street,
			@RequestParam(value="city",required=false)Integer city,
			@RequestParam(value="state",required=false)String state,
			@RequestParam(value="zipCode",required=false)String zipCode,
			@RequestParam(value="country",required=false)Integer country,
			@RequestParam(value="empNo",required=false)Integer empNo,
			@RequestParam(value="annual",required=false)Integer annual,
			@RequestParam(value="desc",required=false)String desc,
			@RequestParam(value="accountType",required=false)Integer accountType,
			@RequestParam(value="industry",required=false)Integer industry,
			@RequestParam(value="email",required=false)String email,*/
			ModelMap modelMap,
			@Valid @ModelAttribute("accountForm") AccountForm accountForm,BindingResult result){
		Employee userDetails = (Employee)session.getAttribute("userDetails");

		if(result.hasErrors()){
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>....."+result.getAllErrors().size());
			model.addAttribute("accountForm", accountForm);
			TYPE[] types =TYPE.values();
			INDUSTRY[] industries = INDUSTRY.values();
			
			
			modelMap.addAttribute("accountForm", accountForm);
			model.addAttribute("industries", industries);
			model.addAttribute("types", types);
			List<Country> countries = new ArrayList<Country>();
			countries =  countryService.getAll();
			model.addAttribute("countries", countries);
			List<City> cities = new ArrayList<City>();
			cities = cityService.getAllCityByCountryId(accountForm.getCountry());
			model.addAttribute("cities",cities);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "new-account";
		}
		/*contact.setDate(new Date());
		if(contactForm.getEmail() != null && !contactForm.getEmail().equalsIgnoreCase("")){
			contact.setEmail(contactForm.getEmail());
		}*/
		
		//Industry industry2 = industeryService.getIndusteryById(industry);
		//Type type = typeService.getTypeById(accountType);
		//Country country2 = countryService.getCountryById(country);
		Account account = new Account();
		account.setAccountCreator(userDetails);
		if(accountForm.getAccountName() != null && !accountForm.getAccountName().equalsIgnoreCase("")){
			account.setAccountName(accountForm.getAccountName());
		}
		if(accountForm.getCompanyName() != null && !accountForm.getCompanyName().equalsIgnoreCase("")){
			account.setCompanyName(accountForm.getCompanyName());
		}
		if(accountForm.getAnnualRevenue() != null){
			account.setAnnualRevenue(accountForm.getAnnualRevenue());
		}
		Integer cityId=accountForm.getCity();
		if(accountForm.getCity()!=null && !cityId.equals(0)){
			account.setCity(cityService.getCityById(accountForm.getCity()));
		}
		if(accountForm.getCountry() != null){
			account.setCountry(countryService.getCountryById((accountForm.getCountry())));
		}
		if(accountForm.getDescription() != null && !accountForm.getDescription().equalsIgnoreCase("")){
			account.setDescription(accountForm.getDescription());
		}
		if(accountForm.getEmail() != null && !accountForm.getEmail().equalsIgnoreCase("")){
			account.setEmail(accountForm.getEmail());
		}
		if(accountForm.getEmpNum() != null){
			account.setEmpNum(accountForm.getEmpNum());
		}
		if(accountForm.getFax() != null && !accountForm.getFax().equalsIgnoreCase("")){
			account.setFax(accountForm.getFax());
		}
		if(accountForm.getIndustry() != null){
			INDUSTRY[] industries=	INDUSTRY.values();
			account.setIndustry(industries[accountForm.getIndustry()].getName());
		}
//		if(accountForm.getParentAccount() != null && !accountForm.getParentAccount().equalsIgnoreCase("")){
//			account.setParentAccountId((accountService.getAccountByName(accountForm.getParentAccount())).getId());
//		}
		if(accountForm.getPhone() != null && !accountForm.getPhone().equalsIgnoreCase("")){
			account.setPhone(accountForm.getPhone());
		}
		if(accountForm.getMobile() != null && !accountForm.getMobile().equalsIgnoreCase("")){
			account.setMobile(accountForm.getMobile());
		}
		if(accountForm.getState() != null && !accountForm.getState().equalsIgnoreCase("")){
			account.setState(accountForm.getState());
		}
		if(accountForm.getStreet() != null && !accountForm.getStreet().equalsIgnoreCase("")){
			account.setStreet(accountForm.getStreet());
		}
		if(accountForm.getType() != null){
			TYPE[] types =TYPE.values();
			account.setType(types[accountForm.getType()].getName());
		}
		if(accountForm.getWebsite() != null && !accountForm.getWebsite().equalsIgnoreCase("")){
			account.setWebSite(accountForm.getWebsite());
		}
		if(accountForm.getZipCode() != null && !accountForm.getZipCode().equalsIgnoreCase("")){
			account.setZipCode(accountForm.getZipCode());
		}
		/*if(city!= null){
			City city2 = cityService.getCityById(city);
			account.setCity(city2);
		}
		account.setAccountName(account_name);
		account.setFax(fax);
		account.setIndustry(industry2);
		account.setPhone(phone);
		if(type != null){
			account.setType(type);
		}
		
		account.setWebSite(website);
		account.setEmail(email);
		account.setAnnualRevenue(annual);
		
		account.setCountry(country2);
		account.setState(state);
		account.setStreet(street);
		account.setZipCode(zipCode);
		account.setDescription(desc);
		account.setEmpNum(empNo);*/
		account.setIsDeleted(false);
		Date date = new Date();
		account.setDateCreated(date);
		account.setDateModified(date);
		accountService.addAccount(account);
		//FollowType followType = followTypeService.getFollowTypeById(1);
//				UpdateType updateType = updateTypeService.getById(5);
				UpdateChatter updateChatter = new UpdateChatter();
				updateChatter.setAction_emp(userDetails);
				updateChatter.setDate(new Date());
				updateChatter.setFollowedId(account.getId());
				updateChatter.setFollowType(FOLLOWTYPE.Account.getType());
				updateChatter.setUpdateType(UPDATETYPE.Added.getName());
				
				Post post = new Post();
				post.setPostDate(new Date());
				post.setPoster(userDetails);
				String postText = "<a href='/sales/s/viewAccount.html?id="+account.getId()+"'>i created Account Name:"+account.getAccountName()+"</a>";
				post.setPostText(postText);
				postService.addPost(post);	
				updateChatter.setPost(post);
				updateChatterService.add(updateChatter);
				model.addAttribute("userDetails",userDetails);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
		return "redirect:/s/accounts.html";
	}	
	
	@RequestMapping(value="/edit-account",method=RequestMethod.GET)
	public String getEditLead(Model model,HttpSession session,HttpServletRequest request){
		if (SecurityUtils.getSubject().hasRole("accountEdit")){
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		
		Integer id = Integer.parseInt(request.getParameter("id").toString());
//		String accountName=request.getParameter("accountName");
//		System.out.println("account is"+accountName);
//		String phone=request.getParameter("phone").toString();
//		String fax = request.getParameter("fax").toString();
//		String email = request.getParameter("email").toString();
//		String street = request.getParameter("street").toString();
//		Integer country = Integer.parseInt(request.getParameter("country").toString());
//		String state = request.getParameter("state").toString();
//		String zipCode = request.getParameter("zipCode").toString();
//		Integer city = Integer.parseInt(request.getParameter("city").toString());
//		String website = request.getParameter("website").toString();
//		Integer empNum = Integer.parseInt(request.getParameter("empNum").toString());
//		Integer annualRevenue = Integer.parseInt(request.getParameter("annualRevenue").toString());
//		String description = request.getParameter("description").toString();
//		Integer type = Integer.parseInt(request.getParameter("type").toString());
//		Integer industry = Integer.parseInt(request.getParameter("industry").toString());
		
		
		Account account = accountService.getAccount(id);
		//account.setAccountName(accountName);
		AccountConverter accountConverter = new AccountConverter();
		AccountForm accountForm = accountConverter.accountConerter(account);
//		accountForm.setId(id);
//		accountForm.setAccountName(accountName);
//		accountForm.setPhone(phone);
//		accountForm.setFax(fax);
//		accountForm.setEmail(email);
//		accountForm.setStreet(street);
//		accountForm.setCountry(country);
//		accountForm.setState(state);
//		accountForm.setZipCode(zipCode);
//		accountForm.setCity(city);
//		accountForm.setWebsite(website);
//		accountForm.setEmpNum(empNum);
//		accountForm.setAnnualRevenue(annualRevenue);
//		accountForm.setType(type);
//		accountForm.setDescription(description);
//		accountForm.setIndustry(industry);
		
		model.addAttribute("userDetails",userDetails);
		TYPE[] types = TYPE.values();
		
		//List<INDUSTRY> industries = accountForm.getIndustry();
		INDUSTRY[] industries = INDUSTRY.values();
		
		/*List<Country> countries = countryService.getAll();
		if(accountForm.getCountry()!= null){
			List<City> cities = cityService.getAllCityByCountryId(accountForm.getCountry());
			model.addAttribute("cities", cities);
		}*/
		
		List<Country> countries = new ArrayList<Country>();
		countries =  countryService.getAll();
		if(accountForm.getCountry()!= null){
			List<City> cities = cityService.getAllCityByCountryId(accountForm.getCountry());
			model.addAttribute("cities", cities);
		}
		model.addAttribute("industries", industries);
		model.addAttribute("countries", countries);
		model.addAttribute("accountForm", accountForm);

		model.addAttribute("types", types);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return"edit-account";
		}
		else{
			return "accessDenied";
		}
	}
	
	@RequestMapping(value="/edit-account",method=RequestMethod.POST)
	public String postEditLead(Model model,HttpSession session,
//			@RequestParam(value="account_name",required=false)String account_name,
//			@RequestParam(value="parentAccount",required=false)String parentAccount,
//			@RequestParam(value="phone",required=false)String phone,
//			@RequestParam(value="fax",required=false)String fax,
//			@RequestParam(value="website",required=false)String website,
//			@RequestParam(value="street",required=false)String street,
//			@RequestParam(value="city",required=false)Integer city,
//			@RequestParam(value="state",required=false)String state,
//			@RequestParam(value="zipCode",required=false)String zipCode,
//			@RequestParam(value="country",required=false)Integer country,
//			@RequestParam(value="empNo",required=false)Integer empNo,
//			@RequestParam(value="annual",required=false)Integer annual,
//			@RequestParam(value="desc",required=false)String desc,
//			@RequestParam(value="accountType",required=false)Integer accountType,
//			@RequestParam(value="industry",required=false)Integer industry,
//			@RequestParam(value="email",required=false)String email,
//			@RequestParam(value="id",required=false)Integer id,
			
			@Valid @ModelAttribute("accountForm") AccountForm accountForm,BindingResult result){
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		INDUSTRY[]  industry2 = INDUSTRY.values();
		TYPE[] types2 = TYPE.values();
//		INDUSTRY industry2 = industeryService.getIndusteryById(industry);
//		Type type = typeService.getTypeById(accountType);
		if(result.hasErrors()){
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>....."+result.getAllErrors().size());
			model.addAttribute("accountForm", accountForm);
			
			model.addAttribute("industries", industry2);
			model.addAttribute("types", types2);
			List<Country> countries = new ArrayList<Country>();
			countries =  countryService.getAll();
			model.addAttribute("countries", countries);
			List<City> cities = new ArrayList<City>();
			if(accountForm.getCountry() != null ){
				 cities = cityService.getAllCityByCountryId(accountForm.getCountry());
			 }
			model.addAttribute("cities",cities);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "edit-account";
		}
		
		Account account = accountService.getAccount(accountForm.getId());
		account.setAccountCreator(userDetails);

		if(accountForm.getAccountName() != null && !accountForm.getEmail().equalsIgnoreCase("")){
			account.setAccountName(accountForm.getAccountName());
		}
		if(accountForm.getCompanyName() != null && !accountForm.getCompanyName().equalsIgnoreCase("")){
			account.setCompanyName(accountForm.getCompanyName());
		}
		if(accountForm.getAnnualRevenue() != null){
			account.setAnnualRevenue(accountForm.getAnnualRevenue());
		}
		Integer cityId=accountForm.getCity();
		if(accountForm.getCity()!=null && !cityId.equals(0)){
			City city2 = cityService.getCityById(accountForm.getCity());
			account.setCity(city2);
		}
		else{
			account.setCity(null);
		}
		if(accountForm.getCountry() != null){
			account.setCountry(countryService.getCountryById((accountForm.getCountry())));
		}
		if(accountForm.getDescription() != null && !accountForm.getDescription().equalsIgnoreCase("")){
			account.setDescription(accountForm.getDescription());
		}
		if(accountForm.getEmail() != null && !accountForm.getEmail().equalsIgnoreCase("")){
			account.setEmail(accountForm.getEmail());
		}
		if(accountForm.getEmpNum() != null){
			account.setEmpNum(accountForm.getEmpNum());
		}
		if(accountForm.getFax() != null && !accountForm.getFax().equalsIgnoreCase("")){
			account.setFax(accountForm.getFax());
		}
		if(accountForm.getIndustry() != null){
			INDUSTRY[] industries=	INDUSTRY.values();
			account.setIndustry(industries[accountForm.getIndustry()].getName());
		}
		if(accountForm.getPhone() != null && !accountForm.getPhone().equalsIgnoreCase("")){
			account.setPhone(accountForm.getPhone());
		}
		if(accountForm.getMobile() != null && !accountForm.getMobile().equalsIgnoreCase("")){
			account.setMobile(accountForm.getMobile());
		}
		if(accountForm.getState() != null && !accountForm.getState().equalsIgnoreCase("")){
			account.setState(accountForm.getState());
		}
		if(accountForm.getStreet() != null && !accountForm.getStreet().equalsIgnoreCase("")){
			account.setStreet(accountForm.getStreet());
		}
		if(accountForm.getType() != null){
			TYPE[] types =TYPE.values();
			account.setType(types[accountForm.getType()].getName());
		}
		if(accountForm.getWebsite() != null && !accountForm.getWebsite().equalsIgnoreCase("")){
			account.setWebSite(accountForm.getWebsite());
		}
		if(accountForm.getZipCode() != null && !accountForm.getZipCode().equalsIgnoreCase("")){
			account.setZipCode(accountForm.getZipCode());
		}
		//Account account = accountService.getAccount(id);
		//account.setAccountCreator(userDetails);
		//account.setAccountName(account_name);
		//account.setFax(fax);
		//account.setIndustry(industry2[industry].getName());
		//account.setPhone(phone);
		//account.setType(types2[accountType].getName());
		//account.setWebSite(website);
		//account.setEmail(email);
		//account.setAnnualRevenue(annual);
		//account.setCity(city2);
		//account.setCountry(country2);
		//account.setState(state);
		//account.setStreet(street);
		//account.setZipCode(zipCode);
		//account.setDescription(desc);
		//account.setEmpNum(empNo);
		account.setIsDeleted(false);
		account.setDateModified(new Date());
		accountService.saveOrUpdate(account);
		//save post to view on chatter page
//				FollowType followType = followTypeService.getFollowTypeById(1);
		
//						UpdateType updateType = updateTypeService.getById(1);
						UpdateChatter updateChatter = new UpdateChatter();
						updateChatter.setAction_emp(userDetails);
						updateChatter.setDate(new Date());
						updateChatter.setFollowedId(account.getId());
						updateChatter.setFollowType(FOLLOWTYPE.Account.getType());
						updateChatter.setUpdateType(UPDATETYPE.Edited.getName());
						
						Post post = new Post();
						post.setPostDate(new Date());
						post.setPoster(userDetails);
						String postText = "<a href='/sales/s/viewAccount.html?id="+account.getId()+"'>i edited Account Name:"+account.getAccountName()+"</a>";
						post.setPostText(postText);
						postService.addPost(post);	
						updateChatter.setPost(post);
						updateChatterService.add(updateChatter);
						model.addAttribute("userDetails",userDetails);
						joinRequestService.getJoinRquestNo(session, model);
						calendarService.getAppointmentEventsNo(session, model);
						calendarService.getCallEventssNo(session, model);
		return"redirect:/s/accounts.html";
	}
	@RequestMapping(value="/delete-account" ,method=RequestMethod.GET)
	public String getDeletePage(HttpServletRequest request,HttpServletResponse response,Model model,HttpSession session) {
		if (SecurityUtils.getSubject().hasRole("accountDelete")){
		Integer id =Integer.parseInt(request.getParameter("id").toString());
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		//save post to view on chatter page
//				FollowType followType = followTypeService.getFollowTypeById(1);
		
//						UpdateType updateType = updateTypeService.getById(5);
						UpdateChatter updateChatter = new UpdateChatter();
						updateChatter.setAction_emp(userDetails);
						updateChatter.setDate(new Date());
						updateChatter.setFollowedId(id);
						updateChatter.setFollowType(FOLLOWTYPE.Account.getType());
						updateChatter.setUpdateType(UPDATETYPE.Added.getName());
						Account account = accountService.getAccount(id);
						Post post = new Post();
						post.setPostDate(new Date());
						post.setPoster(userDetails);
						String postText = "<a href='/sales/s/viewAccount.html?id="+id+"'>i deleted Account Name:"+account.getAccountName()+"</a>";
						post.setPostText(postText);
						postService.addPost(post);	
						updateChatter.setPost(post);
						updateChatterService.add(updateChatter);
						DeletedAccount deletedAccount = new DeletedAccount();
						deletedAccount.setDeletedId(id);
						deletedAccount.setDeleterId(userDetails.getId());
						//Account account = accountService.getAccount(id);
						account.setIsDeleted(true);
						account.setDateModified(new Date());
						accountService.saveOrUpdate(account);
						deletedAccountService.addDeletedAccount(deletedAccount);
		//accountService.deleteAccount(id);
						joinRequestService.getJoinRquestNo(session, model);
						calendarService.getAppointmentEventsNo(session, model);
						calendarService.getCallEventssNo(session, model);
		return"redirect:/s/accounts.html";
		}
		else{
			return "accountDelete";
		}
	}
	@RequestMapping(value="/viewAccount",method=RequestMethod.GET)
	public String viewLead(Model model,HttpSession session,HttpServletRequest request) {
		if (SecurityUtils.getSubject().hasRole("accountView")){
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		 Object object = request.getParameter("b");
		 if(object != null){
			 model.addAttribute("chatter", "chatter");
		 }
		Account account = accountService.getAccount(Integer.parseInt(request.getParameter("id")));
		model.addAttribute("account", account);
		model.addAttribute("userDetails", userDetails);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		joinRequestService.getJoinRquestNo(session, model);
		return "viewAccount";
		}
		else{
			return "accessDenied";
		}
	}
}
